package com.shared.controller;

import com.shared.dao.POrderItemMapper;
import com.shared.dao.POrdersMapper;
import com.shared.dao.PUserMapper;
import com.shared.pojo.PComment;
import com.shared.pojo.POrderItem;
import com.shared.pojo.POrders;
import com.shared.pojo.PUser;
import com.shared.server.PCommentServer;
import com.shared.util.RestDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class PCommentComment {

    @Autowired
    private PCommentServer pCommentServer;
    @Autowired
    private POrderItemMapper pOrderItemMapper;
    @Autowired
    private POrdersMapper pOrdersMapper;
    @Autowired
    private PUserMapper pUserMapper;

    @PostMapping("/comment/{ordersn}")
    @ResponseBody
    @ApiOperation(value = "添加商品评论")
    public RestDTO addComment(@PathVariable("ordersn") String ordersn, @RequestBody PComment pComment) {
        List<POrderItem> pOrderItemList = pOrderItemMapper.findByOrdersn(ordersn);
        for (POrderItem pOrderItem : pOrderItemList) {
            POrders pOrders = pOrdersMapper.findorders(ordersn);
            PUser pUser = pUserMapper.queryById(pOrders.getUserid());
            PComment pComment1 = new PComment();
            pComment1.setGoodsid(pOrderItem.getGoodsid());
            pComment1.setContent(pComment.getContent());
            pComment1.setUsername(pUser.getName());
            pComment1.setUserimg(pUser.getPortrait());
            pComment1.setCommentime(new Date());
            this.pCommentServer.addComment(pComment1);
            this.pOrdersMapper.updateOrderState(ordersn, 4);
            return RestDTO.ADD_SUCCESS;
        }
        return RestDTO.ADD_FAILURE;
    }
}
